package com.example.article;//package com.example.article;
//
//import com.example.common.constants.ArticleConstants;
//import com.example.common.redis.RedisCache;
//
//import java.time.Instant;
//import java.time.LocalDateTime;
//import java.time.ZoneId;
//import java.time.format.DateTimeFormatter;
//
//public class test {
//    public static void main(String[] args) {
//        long timestamp = 1712064847000L; // Assuming the timestamp is in milliseconds
//
//        // Convert timestamp to LocalDateTime
//        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
//
//        // Define date formatting pattern
//        String pattern = "MM/dd/yyyy, hh:mm:ss a";
//
//        // Format the LocalDateTime
//        String formattedDateTime = dateTime.format(DateTimeFormatter.ofPattern(pattern));
//
//        System.out.println(formattedDateTime); // Output: "11/30/2024, 06:54:13 AM"
//    }
//}

import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TimeStampMerger {

    public static List<String> mergeTimeStamps(List<String> timestamps, int thresholdMinutes) {
        List<String> mergedTimeStamps = new ArrayList<>();

        if (timestamps == null || timestamps.isEmpty()) {
            return mergedTimeStamps;
        }

        // Convert timestamps to minutes
        int[] minutes = new int[timestamps.size()];
        for (int i = 0; i < timestamps.size(); i++) {
            minutes[i] = Integer.parseInt(timestamps.get(i).substring(0, 2)) * 60 + Integer.parseInt(timestamps.get(i).substring(3));
        }

        int start = minutes[0];
        int end = start;

        for (int i = 1; i < minutes.length; i++) {
            if (minutes[i] - end <= thresholdMinutes) {
                // Extend the current time range
                end = minutes[i];
            } else {
                // Add the current time range to the result and start a new range
                mergedTimeStamps.add(formatTime(start) + " - " + formatTime(end));
                start = minutes[i];
                end = start;
            }
        }

        // Add the last time range to the result
        mergedTimeStamps.add(formatTime(start) + " - " + formatTime(end));

        return mergedTimeStamps;
    }

    private static String formatTime(int minutes) {
        int hours = minutes / 60;
        int mins = minutes % 60;
        return String.format("%02d:%02d", hours, mins);
    }

    public static void main(String[] args) {
        String pswd = DigestUtils.md5DigestAsHex(("wangwu" + "sdsa").getBytes());
        System.out.println(pswd);
//        List<String> timestamps = Arrays.asList("20:23", "20:24", "20:25", "20:40", "20:41", "20:42");
//        int thresholdMinutes = 5; // Define your threshold here
//
//        List<String> mergedTimeStamps = mergeTimeStamps(timestamps, thresholdMinutes);
//
//        // Print the merged time stamps
//        for (String timestamp : mergedTimeStamps) {
//            System.out.println(timestamp);
//        }
    }
}
